package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;


    /**
     * 订单结算页渲染，处理三种商品类别：会员、专辑、声音
     *
     * @param tradeVo
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "订单结算页渲染，处理三种商品类别：会员、专辑、声音")
    @PostMapping("/orderInfo/trade")
    public Result<OrderInfoVo> tradeData(@Validated @RequestBody TradeVo tradeVo) {
        //1.获取当前用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务层获取订单结算页数据
        OrderInfoVo orderInfoVo = orderInfoService.tradeData(tradeVo, userId);
        return Result.ok(orderInfoVo);
    }


    /**
     * 提交订单（处理余额支付方式，以及微信支付方式），响应订单编号
     *
     * @param orderInfoVo
     * @return {orderNo:"订单唯一编号"}
     */
    @GuiGuLogin
    @Operation(summary = "提交订单（处理余额支付方式，以及微信支付方式），响应订单编号")
    @PostMapping("/orderInfo/submitOrder")
    public Result<Map<String, String>> submitOrder(@Validated @RequestBody OrderInfoVo orderInfoVo) {
        //1.获取当前用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.调用业务层处理订单
        Map<String, String> map = orderInfoService.submitOrder(orderInfoVo, userId);
        return Result.ok(map);
    }


    /**
     * 根据订单编号查询订单信息（包含订单明细列表）
     *
     * @param orderNo
     * @return
     */
    @Operation(summary = "根据订单编号查询订单信息（包含订单明细列表）")
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }

    /**
     * 分页查询当前用户订单列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "分页查询当前用户订单列表")
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    public Result<Page<OrderInfo>> getOrderByUserPage(@PathVariable int page, @PathVariable int limit) {
        //1.获取当前用户ID
        Long userId = AuthContextHolder.getUserId();
        //2.创建分页对象
        Page<OrderInfo> pageInfo = new Page<>(page, limit);
        //3.调用业务层查询
        pageInfo = orderInfoService.getOrderByUserPage(pageInfo, userId);
        return Result.ok(pageInfo);
    }


    /**
     * 用户在线支付成功后，更新订单状态及进行虚拟物品发货
     *
     * @param orderNo 订单编号
     * @return
     */
    @Operation(summary = "用户在线支付成功后，更新订单状态及进行虚拟物品发货")
    @GetMapping("/orderInfo/orderPaySuccess/{orderNo}")
    public Result orderPaySuccess(@PathVariable String orderNo) {
        orderInfoService.orderPaySuccess(orderNo);
        return Result.ok();
    }
}

